# -*- coding: utf-8 -*- 
import wx
import wx.grid
from utility import *

class ResultTable(wx.grid.PyGridTableBase):
    
    def __init__(self, result):
        wx.grid.PyGridTableBase.__init__(self)
        self.data = result
    
    def GetNumberRows(self):
        return len(self.data)-1
    
    def GetNumberCols(self):
        return len(self.data[1])
    
    def GetColLabelValue(self, col):
        return self.data[0][col]
    
    def GetRowLabelValue(self, row):
        return "长度"
    
    def IsEmptyCell(self, row, col):
        return False
    
    def GetValue(self, row, col):
        return self.data[row+1][col]
    
    def SetValue(self, row, col, value):
        pass
      
class ResultGrid(wx.grid.Grid):    
    def __init__(self, parent, data):
        wx.grid.Grid.__init__(self, parent, -1)
        self.SetTable(ResultTable(data))
        
class ResultFrame(wx.Frame) :
    def __init__(self, calculator):
        n = len(calculator.result[0]) + 1
        if n < 12:
            x = 84 * n 
        else:
            x = 800
        y = 110
            
        
        wx.Frame.__init__(self, None, -1, "%s  (单位:%s)"%(calculator.title, settings.unit), size=(x,y), pos=(150,180))
        self.data = calculator.result
        grid = ResultGrid(self, self.data)
        
        mb = wx.MenuBar()

        m = wx.Menu()
        i2 = m.Append(-1, "复制数据标题", "")
        self.Bind(wx.EVT_MENU, self.OnCopy, i2)
        i = m.Append(-1, "复制数据", "")
        self.Bind(wx.EVT_MENU, self.OnCopy, i)
        
        mb.Append(m , "操作")
        
        self.SetMenuBar(mb)

    def OnCopy(self, event):
        mb = self.GetMenuBar()
        itemId = event.GetId()
        item = mb.FindItemById(itemId)
        name = item.GetLabel()
        if name == u'复制数据':s = self.get_data()
        else:
            s = self.get_title()
        data = wx.TextDataObject()
        data.SetText(s)
        if wx.TheClipboard.Open():
            wx.TheClipboard.SetData(data)
            wx.TheClipboard.Close()
        else:pass
        
    def get_data(self):
        r = []
        for row in self.data[1:]:
            i = []
            for item in row:
                i.append(item)
            r.append('\t'.join(i))
        return '\n'.join(r)
                
        
    def get_title(self):
        r = [] 
        for item in self.data[0]:
            r.append(item)
        return '\t'.join(r)
        

